import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author : lizhifeng
 * @create 2022/8/15 16:40
 * @DESC 2022/8/15 16:40
 */


public class DoubleCostCheck {


    private DoubleCostCheck() {
    }

    private List<Item> itemList = new ArrayList<>();

    private static final DoubleCostCheck instance = new DoubleCostCheck();

    public static DoubleCostCheck getInstance() {
        return instance;
    }


    public synchronized void markAsSpent(TransactionId key, String address) {
        Item item = new Item(key, address);
        if (!itemList.contains(item)) {
            itemList.add(item);
        }
    }


    public synchronized boolean checkUsed(TransactionId key, String address) {
        Item item = new Item(key, address);
        return itemList.contains(item);
    }


    public static class Item {

        private TransactionId transactionId;
        private String address;

        public Item(TransactionId key, String address) {
            this.transactionId = key;
            this.address = address;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Item item = (Item) o;
            return Objects.equals(transactionId, item.transactionId) &&
                    Objects.equals(address, item.address);
        }

        @Override
        public int hashCode() {
            return Objects.hash(transactionId, address);
        }

        @Override
        public String toString() {
            return "Item{}";
        }
    }


}
